package com.example.qsindusty.jmm_spline.OrderinfoActivity;

import com.yixin.ystartlibrary.base.BasePresenter;
import com.yixin.ystartlibrary.base.IBaseView;
import com.yixin.ystartlibrary.utils.LogUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Administration on 2018/3/7.
 */

public class OrderInfoPresenter extends BasePresenter implements OrderInfoContact.Perenter {
        OrderInfoContact.View mView ;
    @Override
    public void bindView(IBaseView view) {
            mView = (OrderInfoContact.View) view;
    }

    @Override
    public void unbindView() {
        dispose();
    }

    /**
     * TOKEN：用户登录验证
     XH：订单序号
     ZT：要修改的订单状态（JH：接货，上传接货信息，HD:运输完成,上传回单信息）
     * @param token
     * @param ordernum
     */
    @Override
    public void up(String token, String ordernum, String status, List<File> files, final int code) {
        LogUtils.e("token:"+token+"order:"+ordernum+"status:"+status);
        List<MultipartBody.Part> parts = new ArrayList<>();
        for (int i = 0; i <files.size(); i++) {
            RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), files.get(i));
            MultipartBody.Part part = MultipartBody.Part.createFormData("img"+i,files.get(i).getName(),imageBody);
            parts.add(part);
        }
        LogUtils.i("size="+parts.size()+"token");
/*
        ApiManager.getInstence().getJmmApi().upLoadPic(token,ordernum,status,parts)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<OrderInfoData>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading();
                    }
                    @Override
                    public void onNext(OrderInfoData jsonElement) {
                    if (jsonElement.getStatus().equals("0")){
                        mView.upSuccessful(jsonElement.getMessage(),jsonElement,code);
                    }else{
                        mView.upSuccessful(jsonElement.getMessage(),null,code);
                    }

                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtils.e("json=="+e.getMessage().toString());
                        mView.closeLoading();
                    }

                    @Override
                    public void onComplete() {
                        mView.closeLoading();
                    }
                });
*/
    }



    @Override
    public void upPic(String token, String lx, String ordernum,File uri, final int code) {

        RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), uri);
        MultipartBody.Part part = MultipartBody.Part.createFormData("img",uri.getName(),imageBody);
/*
        ApiManager.getInstence().getJmmApi().upLoadGoodsPic(token,lx,ordernum,part)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<OrderInfoData>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(OrderInfoData orderInfoData) {
                        LogUtils.i(orderInfoData.toString());
                        if (TextUtils.equals(orderInfoData.getStatus(),"0"))

                            mView.upSuccessful(orderInfoData.getMessage(),orderInfoData,code);
                        else
                            mView.upFraier(orderInfoData.getMessage());
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.closeLoading();
                        mView.upFraier(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.closeLoading();
                    }
                });
*/
    }



}

